<%--
  Created by IntelliJ IDEA.
  User: Lin
  Date: 2021/4/21
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>医生看诊</title>
    <link rel="stylesheet" href="${pageContext.servletContext.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.servletContext.contextPath}/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="${pageContext.servletContext.contextPath}/static/layui/layui.js"></script>
    <%--    <script src="${pageContext.servletContext.contextPath}/static/layui/css/modules/laydate/default/laydate.css"></script>--%>
</head>
<body>
<!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->


<div class="layui-form-item layui-form-text" style="margin-left: 38px">

    <button type="button" class="layui-btn" onclick="choose()">选择用药</button>
</div>

<table id="drug" name="drug" lay-filter="drug"></table>

<div class="layui-form-item layui-form-text" style="width: 324px">
    <label class="layui-form-label">诊断结果</label>
    <div class="layui-input-block">
        <textarea name="illnessDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
    </div>
</div>
<%--    <div class="layui-form-item layui-form-text" style="width: 324px">--%>
<%--        <label class="layui-form-label">用药信息</label>--%>
<%--        <div class="layui-input-block">--%>
<%--            <textarea name="illnessDesc" placeholder="请输入内容" class="layui-textarea"></textarea>--%>
<%--        </div>--%>
<%--    </div>--%>
<input type="hidden" name="medicine"/>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button type="button" class="layui-btn" id="submit">看诊结束
        </button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</div>
<!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">分配</button>
    </div>
</script>

<%--<script src="${pageContext.servletContext.contextPath}/static/layui/lay/modules/laydate.js"></script>--%>
<script>

    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };

        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }

        return fmt;
    }
    $(() => {
        //提交诊断信息
        //诊断结束
        $("#submit").on("click", function () {
            let form1 = layui.form;
            let val = form1.val("makeAnAppointment");
            console.log(val);

            if (!sendEndVisit()) {
                return;
            }

            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index);
        });
    });

    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            toolbar: "#toolbarDemo",
            elem: '#drug'
            , height: 312
            , url: '${pageContext.servletContext.contextPath}/medicine/getAllMedicine' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: "checkbox", fixed: "left"},
                {field: 'MEDICINENUM', title: '药品编号', minWidth: 113, sort: true}
                , {field: 'MEDICINENAME', title: '药品名称', minWidth: 104}
                , {
                    field: "NUM", title: "取出数量", minWidth: 104, edit: "text",
                    templet: function (obj) {

                        if (obj.NUM == undefined) {
                            obj.NUM = 1;
                            return 1;
                        } else {
                            return obj.NUM;
                        }
                    }
                }
                , {field: 'MANFACTURER', title: '生产商', minWidth: 92, sort: true}
                , {field: 'MAINEFFECT', title: '主要功效', minWidth: 99}
                , {
                    field: 'MANUFACTUREDATE', title: '生产日期', minWidth: 136,
                    templet: function (obj) {
                        let date = new Date();
                        date.setTime(obj.MANUFACTUREDATE);
                        return date.format("yyyy年mm月dd日")
                    }
                }
                , {field: 'TABOOS', title: '禁忌', width: 84, sort: true}
                , {field: 'INVENTORY', title: '数量', width: 80, sort: true}
                , {field: 'UNITDESC', title: '单位', width: 80}
            ]]
        });
        hide();

        table.on('toolbar(drug)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            console.log(checkStatus);
            switch (obj.event) {
                case 'add':
                    let data = [];
                    for (let temp in checkStatus.data) {
                        if (checkStatus.data[temp].NUM == undefined) {
                            checkStatus.data[temp].NUM = 1;
                        }
                        let info = {
                            id: checkStatus.data[temp].ID,
                            num: checkStatus.data[temp].NUM
                        };
                        data.push(info);
                    }
                    $("[name='medicine']").val(JSON.stringify(data));
                    layer.msg('添加成功');
                    console.log("到这了")
                    break;
            }
        });

    });

    let isShow = false;

    function choose() {
        if (isShow == true) {
            hide();
        } else {
            show();
        }
    }

    function hide() {
        $("[lay-id='drug']").hide();
        isShow = false;
    }

    function show() {
        $("[lay-id='drug']").show();
        isShow = true;
    }

    function sendEndVisit() {
        let diagnosisResult = $("[name='illnessDesc']").val();
        let medicineInfo = $("[name='medicine']").val();
        let Data;

        $.ajax({
            url: "${pageContext.servletContext.contextPath}/reserve/endVisit",
            data: {
                diagnosisResult: diagnosisResult,
                medicineInfo: medicineInfo
            },
            type: "POST",
            dataType: "json",
            async: false,
            success: function (data) {
                console.log(data);
                Data = data;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中
                // 只有一个会包含信息
                //this; // 调用本次AJAX请求时传递的options参数
            }
        });
        if (Data == undefined) {
            layer.msg("异常");
            return false;
        }
        if (Data.code == 0) {
            return true;
        }
        layer.msg(Data.msg);
        return false;
    }
</script>
</body>
</html>
